
<template>
	<div>
		<InputOutputDefine
			:versionId="props.id"
			:stepCode="props.nodeId"
			:inputFlag="true"
			:topFlag="true"
			:getResource="getResource"
			ref="input"
		>
		</InputOutputDefine>

		<a-row>
			<a-col span="4">
				<a-select
					size="large"
					style="width: 100%"
					v-if="props.componentConfig?.input"
					v-model:value="props.componentConfig.input.dataSourceId"
					placeholder="请选择"
					allowClear
					show-search
				>
					<a-select-option
						v-for="item in dataSourceList"
						:key="item.id"
						:label="item.datasourceName"
						:value="item.id"
					/>
				</a-select>
			</a-col>
			<a-col span="4"> </a-col>
			<a-col span="16">
				<Checkbox v-if="props.componentConfig?.input" v-model="props.componentConfig.input.pageFlag">分页</Checkbox>
			</a-col>
		</a-row>

		<a-row>
			<a-col span="24">
				<SqlRunner :param="props.componentConfig.input" :getRunArgs="getRunArgs"></SqlRunner>
			</a-col>
		</a-row>
	</div>
</template>
<script setup>
import { onMounted, ref, watch } from 'vue'
import SqlRunner from '@/views/datasource/sqlRunner.vue'
import dataSourceApi from '@/api/datasource/dataSourceApi'
import InputOutputDefine from './InputOutputDefine.vue'
import { mergeStepConfigToFrontFile } from '@/utils'

const dataSourceList = ref([])
const input = ref()

const defaultConfig = () => {
	return {
		input: {
			dataSourceId: null,
			schemaId: null,
			tableName: null,
			sql: null,
			pageFlag: false
		}
	}
}

const getResource = () => {
	let frontFile = mergeStepConfigToFrontFile(props.frontFile, props.nodeId, props.componentConfig)
	return frontFile
}

const props = defineProps(['componentConfig', 'nodeId', 'id', 'frontFile'])

const queryList = async () => {
	const data = await dataSourceApi.queryList()
	dataSourceList.value = data
}

onMounted(async () => {
	if (!hasProperties(props.componentConfig)) {
		const c = defaultConfig()
		props.componentConfig.input = c.input
	}

	await queryList()
})

const hasProperties = (obj) => {
	if (!obj) {
		return false
	}

	return JSON.stringify(obj) != '{}'
}

const getRunArgs = () => {
	if (input.value.getParams) {
		return input.value.getParams()
	}
	return {}
}
//现在开始处理
</script>


<style scoped></style>
